<?php namespace module\material;

use module\HdSite;
use system\model\MaterialNews;

class Site extends HdSite {

    //图片素材列表
    public function AdminImageLists() {
        $data = Db::table( 'material_images' )->paginate( 8 );
        View::with( 'data', $data );
        $this->display( 'Imglist' );
    }

    //将图片推送到微信
    public function AdminPushImage() {
        $file = q( 'post.file' );
        $res  = Wx::instance( 'Material' )->upload( 'image', $file );
        if ( is_array( $res ) and isset( $res['media_id'] ) ) {
            $data['path']     = $file;
            $data['media_id'] = $res['media_id'];
            $data['wx_path']  = $res['url'];
            Db::table( 'material_images' )->insert( $data );
            ajax( [ 'valid' => 1, 'message' => '上传成功', 'data' => $data ] );
        }
    }


//    图文素材
    public function AdminNewsLists(){
        $data = Db::table('material_news')->paginate(8);
        View::with('data',$data);
        $this->display('news_lists');
    }

//    图文素材添加
    public function AdminNewsPost(){
        if(IS_POST){
//            p($_POST);exit;
            $res = Wx::instance( 'material' )->addNews( $_POST['data'] );

            if ( is_array( $res ) && $res['media_id'] ) {
                $model             = new MaterialNews();
                $model['content']  = Request::post( 'data' );
                $model['media_id'] = $res['media_id'];
                $model->save();
                message( '保存成功', site_url( 'site/newsLists' ), 'success' );
            }
            message( '推送失败', '', 'error' );
        }
        $this->display('news_post');
    }

    public function admintest() {
        $data=<<<str
{
   "filter":{
      "is_to_all":true
   },
   "mpnews":{
      "media_id":"bAO_11pHEERUYa_2VTD-d4Y-V6lC7TaSvhdH_T9TjMY"
   },
    "msgtype":"mpnews"
}
str;
        $res  = Wx::instance( 'message' )->sendall( $data );
        dd( $res );
    }



}